#include "stdafx.h"


struct shape_util
{
    sf::ConvexShape Polygon;
    //sf::Shape Polygon;
    sf::RenderWindow * window;

    map<string, sf::Sprite> sprites;
    map<string, sf::Texture> textures;
    std::vector <Vector2f> points;
    Vector2f mouse;
    void init()
    {
        window = new sf::RenderWindow (sf::VideoMode(800, 600), "procedural shape utility");

        window->setMouseCursorVisible(false);

        textures["cursor"] = sf::Texture();
        textures["bg"] = sf::Texture();
        sprites["cursor"] = sf::Sprite();
        sprites["bg"] = sf::Sprite();

        textures["bg"].loadFromFile("grid.png");
        textures["cursor"].loadFromFile("cursor.png");
        sprites["bg"].setTexture(textures["bg"]);
        sprites["cursor"].setTexture(textures["cursor"]);

        Polygon.setFillColor(sf::Color(0,0,0,0));
        Polygon.setOutlineColor(sf::Color(30,100,200));
        Polygon.setOutlineThickness(1.5);
    }
    ~shape_util() { delete window; }
    void handle_loop()
    {
        while (window->isOpen())
        {
            sf::Event event_;
            while (window->pollEvent(event_))
            {
                if (event_.type == sf::Event::Closed)// || event.type==sf::Event::KeyPressed)
                    window->close();
                if(event_.type == sf::Event::KeyPressed)// || event.type == sf::Event::KeyReleased)
                    switch(event_.key.code) { case Keyboard::Escape: window->close(); break; }
                if(event_.type == sf::Event::MouseMoved)
                {
                    mouse = Vector2f(float(event_.mouseMove.x), float(event_.mouseMove.y));
                    if(Polygon.getPointCount() > 0)
                    Polygon.setPoint(Polygon.getPointCount()-1, mouse);
                }
                if(event_.type == sf::Event::MouseButtonPressed)
                {
                    points.clear();
                    FOR(Polygon.getPointCount())
                        points.push_back(Polygon.getPoint(i));
                    points.push_back(mouse);
                    Polygon.setPointCount(points.size());
                    FOR(points.size())
                        Polygon.setPoint(i, points[i]);

                    //size_t u = Polygon.getPointCount();
                    //cout << "point begin" << endl;
                    //FOR(u)
                    ////    printf("%f %f\n", Polygon.getPoint(i).x, Polygon.getPoint(i).y);
                    //    cout << "    point " << i << ": " <<
                    //        Polygon.getPoint(i).x << " " << Polygon.getPoint(i).y << endl;
                    //cout << "point end" << endl;

                }
                if(event_.type == sf::Event::MouseButtonReleased) {}
                if(event_.type == sf::Event::KeyPressed) {}
                if(event_.type == sf::Event::KeyReleased) {}
                
                window->clear();
                window->draw(sprites["bg"]);
                window->draw(Polygon);
                window->draw(sprites["cursor"]);
                window->display();

            }
        }
    }

};